const OrderList = require('../model/order_list');

module.exports = {

  /**
   * 添加订单明细的中间件
   * @param request
   * @param response
   * @param next
   */
  sOrderList: (request, response, next) => {
    let {order_code, goods_id, goodsprice, goodscount, price} = request.body

    // console.log('order_code = ' + order_code + '; goods_id = ' + goods_id +
    //   '; goodsprice = ' + goodsprice + '; goodscount = ' + goodscount + '; price = ' + price);

    let order_list = {
      order_code, goods_id, goodsprice, goodscount, price
    }
    OrderList.setOrderList(order_list)
      .then(result => {
        request.add_olid = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取订单明细中间件
   * @param request
   * @param response
   * @param next
   */
  gOrderList: (request, response, next) => {
    let {order_code} = request.body

    OrderList.getOrderList(order_code)
      .then(result => {
        request.orderDetail = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定订单明细条数
   * @param request
   * @param response
   * @param next
   */
  gTransactionCount: (request, response, next) => {
    let {id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate} = request.body

    OrderList.getTransactionCount(id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate)
      .then(result => {
        request.orderListCount = result
        next()
      })
      .catch(error => {
        next(error)
      })
  },

  /**
   * 获取指定条数的订单明细
   * @param request
   * @param response
   * @param next
   */
  gTransactionList: (request, response, next) => {
    let start = response.start
    let size = response.size
    let {id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate} = request.body
    OrderList.getTransactionList(start, size, id, order_code, buy_username, bus_username, goodsCategoryId, rootCategoryId, lessPrice, morePrice, lessDate, moreDate)
      .then(result => {
        request.recordCountOrderList = result
        next()
      })
      .catch(error => {
        next(error)
      })
  }


}
